#!/usr/bin/env python

import socket
socket.setdefaulttimeout(2)

from urllib2 import urlopen
from random import choice

class PlaylistParser:
    def get_entry(self, url):
        try:
            url = urlopen(url)
        except Exception, e:
            print 'Exception', e
            return None
        data = self.clean(url.readlines())
        url.close()
        return choice(data)

    def clean(self, data):
        parsed_data = list()
        for line in data:
            if line.startswith('http'):
                parsed_data.append(line.strip())
            elif line.startswith('File'):
                parsed_data.append(line.split('=')[1].strip())
        return parsed_data
